﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class ReturnToFactoryCheckForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }
        }

        protected void saveLinkButton_Click(object sender, EventArgs e)
        {
            ReturnToFactoryService service = new ReturnToFactoryService();
            ApplicationService appservice = new ApplicationService();
            ReturnToFactoryForm form = service.GetReturnToFactoryFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));
            if (!form.ApplicationProductReference.IsLoaded)
            {
                form.ApplicationProductReference.Load();
            }
            if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
            {
                form.ApplicationProduct.ApplicationFormReference.Load();
            }
            if (!form.ApplicationProduct.ApplicationForm.ApplicationProduct.IsLoaded)
            {
                form.ApplicationProduct.ApplicationForm.ApplicationProduct.Load();
            }

            form.CheckDate = Convert.ToDateTime(checkDateTxtBox.Text);
            form.ReceiveStatus = CheckStatusRadioButtonList.SelectedValue;
            form.ReceiveRemark = checkRamrkTxtBox.Text;
            if (CheckStatusRadioButtonList.SelectedIndex == 0)
            {
                form.ApplicationProduct.Status = "维修完毕待发货";
                form.ApplicationProduct.RepairCompleteDateTime = DateTime.Now;
                form.Status = "返厂-维修完毕";
                ApplicationProductService productService = new ApplicationProductService();
                productService.FinishMaintainProduct(form.ApplicationProduct);
            }
            // 二次返厂
            else
            {
                form.ApplicationProduct.Status = "返厂-待发货";
                form.IsSecondTimeRepair = true;
                form.Status = "返厂-待发货";
            }
            service.Update(form);

            // update application form
            //ApplicationProductService productservice = new ApplicationProductService();
            //productservice.FinishMaintainProduct(form.ApplicationProduct);

            ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('信息保存成功!');location.href='ReturnToFactoryCheckList.aspx';", true);
        }

        private void InitialPage()
        {
            ReturnToFactoryService service = new ReturnToFactoryService();
            ReturnToFactoryForm form = service.GetReturnToFactoryFormProductDetail(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));

            ProviderService providerservice = new ProviderService();
            Provider provider = providerservice.GetProviderByCode(form.ProvideCode);
            if (provider != null)
            {
                providerNameLabel.Text = provider.ProviderName;
                connectPersonLabel.Text = provider.Contact1Name;
                if (!string.IsNullOrEmpty(provider.Contact2Name))
                {
                    connectPersonLabel.Text += "; " + provider.Contact2Name;
                }
                proviceCodeLabel.Text = provider.ProviderCode;
                addressLabel.Text = provider.Address;
                phoneNumberLabel.Text = provider.Contact1Phone;
                if (!string.IsNullOrEmpty(provider.Contact2Phone))
                {
                    phoneNumberLabel.Text += "; " + provider.Contact2Phone;
                }
                cellPhoneLabel.Text = provider.Contact1MobilePhone;
                if (!string.IsNullOrEmpty(provider.Contact2MobilePhone))
                {
                    cellPhoneLabel.Text += "; " + provider.Contact2MobilePhone;
                }

                workStatusLabel.Text = provider.Relation;
            }

            rmaCodeLabel.Text = form.ApplicationProduct.ApplicationForm.ApplicationCode;
            applicantEmailLabel.Text = form.ApplicationProduct.ApplicationForm.User.UserEmail;
            applicantLabel.Text = form.ApplicationProduct.ApplicationForm.User.FullName;
            applicantPhoneLabel.Text = form.ApplicationProduct.ApplicationForm.User.UserPhone;

            rtfRunningNumberLabel.Text = form.RunningNumber;
            facotryRMALabel.Text = form.Code;
            reasonOfCostLabel.Text = form.ApplicationProduct.ReasonOfCost;
            if (form.ApplicationProduct.IsDecideToRepair.HasValue)
            {
                isRepairLabel.Text = form.ApplicationProduct.IsDecideToRepair.Value ? "是" : "否";
            }
            rtfDateTimeLabel.Text = form.ApplyDate.Value.ToString("yyyy-MM-dd");
            productTypeLabel.Text = form.ApplicationProduct.ProductType.ProductTypeName;
            versionLabel.Text = form.ApplicationProduct.ProductVersionNumber;
            productSerialNumberLabel.Text = form.ApplicationProduct.ProductSerialNumber;
            problemDescriptionLabel.Text = form.ApplicationProduct.ProductProblemDescription;
            //receiveTimeLabel.Text = form.ApplicationProduct.ApplicationForm.ReceiveDateTime.Value.ToString("yyyy-MM-dd");
            confirmQuatationLabel.Text = form.ConfirmQuatationDate.HasValue ? form.ConfirmQuatationDate.Value.ToString("yyyy-MM-dd") : "";
            quatationTextBox.Text = form.ProviderQuatationDate.HasValue ? form.ProviderQuatationDate.Value.ToString("yyyy-MM-dd") : "";

            double cost = 0;
            if (!form.ReturnToFactoryQuatation.IsLoaded)
            {
                form.ReturnToFactoryQuatation.Load();
            }
            int index = 0;
            foreach (ReturnToFactoryQuatation quatationItem in form.ReturnToFactoryQuatation)
            {
                index++;
                cost += quatationItem.Count * (double)quatationItem.UnitPrice;
                repairAccessoryTextBox.Text += quatationItem.AccessoryName;
                if (index < form.ReturnToFactoryQuatation.Count)
                {
                    repairAccessoryTextBox.Text += "; ";
                }
            }
            costLabel.Text = cost.ToString();
            checkDateTxtBox.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }
    }
}
